<?php

namespace App\Console\Commands;

use App\Models\Order;
use Illuminate\Console\Command;

class OverTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:overtime';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //创建时间超过 30 分钟且未支付的订单
        $unpaidOrders = Order::where('status', 0)
            //subMinutes(30) 方法是将当前时间往前推 30 分钟
            ->where('created_at', '<', date('Y-m-d H:i:s', time() - 30*60))
            ->get();
            //php artisan schedule:work
        //并将这些订单的状态设置为已取消
        foreach ($unpaidOrders as $order) {
            $order->status = 5;
            $order->save();
        }
    }
}
